package com.ruoyi.project.jiaqin.news.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.membernewslikes.domain.MemberNewsLikes;
import com.ruoyi.project.jiaqin.membernewslikes.service.IMemberNewsLikesService;
import com.ruoyi.project.jiaqin.news.domain.News;
import com.ruoyi.project.jiaqin.news.mapper.NewsMapper;
import com.ruoyi.project.jiaqin.news.service.INewsService;
import com.ruoyi.project.jiaqin.newsfollow.domain.NewsFollow;
import com.ruoyi.project.jiaqin.newsfollow.mapper.NewsFollowMapper;
import com.ruoyi.project.jiaqin.newsimg.domain.NewsImg;
import com.ruoyi.project.jiaqin.newsimg.service.INewsImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 新闻(文章新闻,视频新闻)Service业务层处理
 *
 * @author admin
 * @date 2020-12-11
 */
@Service
public class NewsServiceImpl implements INewsService
{
    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private IMemberNewsLikesService iMemberNewsLikesService;

    /**
     * 查询新闻(文章新闻,视频新闻)
     *
     * @param id 新闻(文章新闻,视频新闻)ID
     * @return 新闻(文章新闻,视频新闻)
     */
    @Override
    public News selectNewsById(String id)
    {
        return newsMapper.selectNewsById(id);
    }

    /**
     * 查询新闻(文章新闻,视频新闻)列表
     *
     * @param news 新闻(文章新闻,视频新闻)
     * @return 新闻(文章新闻,视频新闻)
     */
    @Override
    public List<News> selectNewsList(News news)
    {
        return newsMapper.selectNewsList(news);
    }

    @Override
    public PageInfo<News> getNewss(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> list = newsMapper.getNewss(new News());
        PageInfo<News> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<News> getNewssByTypeVideo(Integer pageNum, Integer pageSize,  String newsClassId) {
        News news = new News();
        news.setType(1);
        news.setNewsClassId(newsClassId);
        PageHelper.startPage(pageNum,pageSize);
        List<News> list = newsMapper.getNewss(news);
        PageInfo<News> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public PageInfo<News> getNewssByTypeArticle(Integer pageNum, Integer pageSize) {
        News news = new News();
        news.setType(0);
        PageHelper.startPage(pageNum,pageSize);
        List<News> list = newsMapper.getNewss(news);
        for (News N : list) {
            NewsImg newsImg = new NewsImg();
            newsImg.setNewsId(N.getId());
            List<NewsImg> newsImgs = newsImgService.selectNewsImgList(newsImg);
            N.setArticleCovers(newsImgs.get(0).getPictureUrl());
        }
        PageInfo<News> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public int likesNews(String userId, String newsId) {
        MemberNewsLikes memberNewsLikes = new MemberNewsLikes();
        memberNewsLikes.setMemberId(userId);
        memberNewsLikes.setNewsId(newsId);
        //点赞表插入数据
        iMemberNewsLikesService.insertMemberNewsLikes(memberNewsLikes);
        //新闻表点赞数量+1
        return newsMapper.updateLikes(1L,newsId);
    }

    @Override
    public int unlikesNews(String userId, String newsId) {
        MemberNewsLikes memberNewsLikes = new MemberNewsLikes();
        memberNewsLikes.setMemberId(userId);
        memberNewsLikes.setNewsId(newsId);
        //点赞表删除数据
        iMemberNewsLikesService.deleteMemberNewsLikesById(userId,newsId);
        return newsMapper.updateLikes(-1L,newsId);
    }

    /**
     * 新增新闻(文章新闻,视频新闻)
     *
     * @param news 新闻(文章新闻,视频新闻)
     * @return 结果
     */
    @Override
    public int insertNews(News news)
    {
        if (news.getId() == null) {
            news.setId(UUID.randomUUID().toString().replace("-",""));
        }
        return newsMapper.insertNews(news);
    }

    /**
     * 修改新闻(文章新闻,视频新闻)
     *
     * @param news 新闻(文章新闻,视频新闻)
     * @return 结果
     */
    @Override
    public int updateNews(News news)
    {
        return newsMapper.updateNews(news);
    }

    /**
     * 删除新闻(文章新闻,视频新闻)对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteNewsByIds(String ids)
    {
        return newsMapper.deleteNewsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除新闻(文章新闻,视频新闻)信息
     *
     * @param id 新闻(文章新闻,视频新闻)ID
     * @return 结果
     */
    @Override
    public int deleteNewsById(String id)
    {
        return newsMapper.deleteNewsById(id);
    }

    @Autowired
    private NewsFollowMapper newsFollowMapper;
    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsImgService newsImgService;
    @Override
    public PageInfo<NewsFollow> getCollectNewsList(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<NewsFollow> newsFollowList = newsFollowMapper.selectNewsFollowByMemberId(memberId);
        if(newsFollowList != null && newsFollowList.size()!= 0){
            for (NewsFollow NF :newsFollowList) {
                News newsById = newsService.selectNewsById(NF.getNewsId());
                if(newsById != null) {
                    NF.setNews(newsById);
                    if (newsById.getMemberId() != null) {
                        NewsImg newsImg = new NewsImg();
                        newsImg.setNewsId(newsById.getId());
                        List<NewsImg> newsImgList = newsImgService.selectNewsImgList(newsImg);
                        if (newsImgList.size() > 0) {
                            newsById.setNewsImg(newsImgList);
                        }
                    }
                }
            }
        }
        PageInfo<NewsFollow> pageInfo = new PageInfo<>(newsFollowList);
        return pageInfo;
    }

    @Override
    public List<News> selectNewsListByNumSee(News news) {
        return newsMapper.selectNewsListByNumSee(news);
    }

    @Override
    public PageInfo<News> getNewsListByMemberId(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<News> list = newsMapper.getNewsListByMemberId2(memberId);
        if(list != null && list.size()!= 0){
            for (News N : list) {
                News newsById = newsService.selectNewsById(N.getId());
                if(newsById != null) {
                    N.setNews(newsById);
                    if (newsById.getMemberId() != null) {
                        NewsImg newsImg = new NewsImg();
                        newsImg.setNewsId(newsById.getId());
                        List<NewsImg> newsImgList = newsImgService.selectNewsImgList(newsImg);
                        if (newsImgList.size() > 0) {
                            newsById.setNewsImg(newsImgList);
                        }
                    }
                }
            }
        }
        PageInfo<News> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int getAllNum(String memberId) {
        return newsMapper.getAllNum(memberId);
    }
}
